<?php

function __autoload($class_name)
{
	global $ti;
	if(empty($ti->path_cache[$class_name]))
	{
		for($i = (count($ti->config['var.namespaces'])-1);$i >=0; $i--)
		{
			$ns = $ti->config['var.namespaces'][$i];
			$nocontinue = _autoload($class_name,$ti->paths['server'].'/'.$ns,$class_name);
			if($nocontinue)
			{
				break;
			}
		}
	}
	include($ti->path_cache[$class_name]);
}

function _autoload($class_name,$path,$orig_name)
{
	global $ti;
	#echo('looking for '.$class_name.': '.$path.'/'.$class_name.".php\n<br />");
	#echo('also checking in '.$path.'/'.$class_name.'/'."\n<br />");
	if(file_exists($path.'/'.$class_name.'.php'))
	{
		$ti->path_cache[$orig_name] = $path.'/'.$class_name.'.php';
		return true;
	}
	else if(is_dir($path.'/'.$class_name))
	{
		$ti->path_cache[$orig_name] = $path.'/'.$class_name.'/'.$class_name.'.php';
		return true;
	}
	else
	{
		$class_name = explode('_',$class_name);
		if(count($class_name) == 1)
		{
			return false;
		}
		$path .= '/'.array_shift($class_name);
		if(is_dir($path))
		{
			return _autoload(implode('_',$class_name),$path,$orig_name);
		}
		else
		{
			return false;
		}
	}
}
?>